package ca.uvic.cs.cogz.mapping.ui;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTree;

import ca.uvic.cs.cogz.mapping.ontology.DataLoaderFacade;

public class NeighborhoodControllerPanel extends JPanel {
	private static final long serialVersionUID = -323291215918448159L;

	private JSplitPane neighborhoodSplitPane;

	private NeighborhoodPanel sourceNeighborhood;
	private NeighborhoodPanel targetNeighborhood;

	public NeighborhoodControllerPanel(JTree sourceTree, JTree targetTree) {
		init(sourceTree, targetTree);
	}

	public void showNeighborhoods() {
		sourceNeighborhood.showNeighborhood();
		targetNeighborhood.showNeighborhood();
	}

	private void init(JTree sourceTree, JTree targetTree) {
		setLayout(new BorderLayout());

		add(getNeighborhoodSplitPane(sourceTree, targetTree), BorderLayout.CENTER);
	}

	private JSplitPane getNeighborhoodSplitPane(JTree sourceTree, JTree targetTree) {
		if (neighborhoodSplitPane == null) {
			neighborhoodSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
			neighborhoodSplitPane.setResizeWeight(0.5);

			DataLoaderFacade ontologyFacade = DataLoaderFacade.getInstance();

			sourceNeighborhood = new NeighborhoodPanel(sourceTree, ontologyFacade.getSourceOntology(), ontologyFacade.getSuggestionsManager(), ontologyFacade.getTemporaryMappingManager());
			targetNeighborhood = new NeighborhoodPanel(targetTree, ontologyFacade.getTargetOntology(), ontologyFacade.getSuggestionsManager(), ontologyFacade.getTemporaryMappingManager());

			neighborhoodSplitPane.add(sourceNeighborhood);
			neighborhoodSplitPane.add(targetNeighborhood);
		}

		return neighborhoodSplitPane;
	}
}
